from app.datatype.user_collection import UserCollectionCreateMdl, UserCollection, UserCollectionGet
from app.initializer import g
from app.utils import db_async


class UserCollectionCreateBiz(UserCollectionCreateMdl):

    async def create(self):
        async with g.db_async_session() as session:
            await db_async.create(
                session=session,
                model=UserCollection,
                data={
                    "user_id": self.user_id,
                    "milvus_index_id": self.milvus_index_id,
                }
            )


class UserCollectionFindBiz(UserCollectionGet):

    async def select_now_all(self):
        async with g.db_async_session() as session:
            return await db_async.query_all(
                session=session,
                model=UserCollection,
                filter_by={"is_deleted": 0, "user_id": self.user_id},
                fields=self.response_fields(),
            )
